package es.ats.codington.festival.daos;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementSetter;




import es.ats.codington.festival.domain.EventSignUp;
import es.ats.codington.festival.domain.Event;
import es.ats.codington.festival.interfaces.daos.IEvenSignUpDAO;

import es.ats.codington.festival.mappers.EventMapper;
import es.ats.codington.festival.mappers.EventSignUpMapper;

public class EventSignUpDAOImpl implements IEvenSignUpDAO{

	//-------------------------------------
	
	private Connection connection = null;
	
	private JdbcTemplate jdbcTemplateObject;
	private Properties queryProperties;

	 
	
	
	/**
	 * --------------------------Constructor------------
	 * @param connection
	 */
	public EventSignUpDAOImpl(Connection connection){
		this.connection=connection;
	}
	
	/**
	 * 
	 * @param jdbcTemplateObject
	 * @param queryProperties
	 */
	
	public EventSignUpDAOImpl(JdbcTemplate jdbcTemplateObject,Properties queryProperties ) {
		this.jdbcTemplateObject = jdbcTemplateObject;
		this.queryProperties = queryProperties;
	}
	
	
	//--------------------------- Methods-------------------
	
	/**
	 * register a visitor in one event.
	 * return the affected rows
	 * 
	 * @param e
	 * @return
	 * @throws SQLException
	 */
	public int registerVisitorForNewEventDAO(final EventSignUp e) throws SQLException{

		
		int affectedRows = jdbcTemplateObject.update(queryProperties.getProperty("registerVisitorToEvent"), 
				new PreparedStatementSetter(){
			@Override
			public void setValues(PreparedStatement statement)
					throws SQLException {
				statement.setInt(1, e.getIdEvent());
				statement.setInt(2, e.getIdVisitor());
			}
		});
		
			return affectedRows;
		
	}
	
	/**
	 * check if a visitor is registered in one event.
	 * return true if is already registered
	 * 
	 * @param e
	 * @return
	 * @throws SQLException
	 */
	public boolean checkEventsofVisitorDAO(final EventSignUp e) throws SQLException{
				
		List<EventSignUp> eventSignUp =  jdbcTemplateObject.query(queryProperties.getProperty("IsRegisteredToEvent"),new PreparedStatementSetter(){
			@Override
			public void setValues(PreparedStatement statement)
					throws SQLException {
				statement.setInt(1, e.getIdEvent());
				statement.setInt(2, e.getIdVisitor());
			}
		}, new EventSignUpMapper());
		
		return eventSignUp.size() >= 1;
		
	}
	
	/**
	 * 
	 * unregister a visitor for one event.
	 * return the affected rows
	 * 
	 * @param e
	 * @return
	 * @throws SQLException
	 */
	public int unregisterVisitorForEventDAO(final EventSignUp e) throws SQLException{
				
		int affectedRows = jdbcTemplateObject.update(queryProperties.getProperty("unRegisterVisitorToEvent"), 
				new PreparedStatementSetter(){
			@Override
			public void setValues(PreparedStatement statement)
					throws SQLException {
				statement.setInt(1, e.getIdEvent());
				statement.setInt(2, e.getIdVisitor());
			}
		});
		
		return affectedRows;

	}
	
	/**
	 * 
	 * return a List with all the registrated events of one user
	 * 
	 * @param e
	 * @return
	 * @throws SQLException
	 */
	public List<Event> viewEventsOfUserDAO(EventSignUp e) throws SQLException{
		
		List<Event> events = new ArrayList<Event>();
		
		events=jdbcTemplateObject.query(queryProperties.getProperty("events_of_one_user"),
				new Object[]{e.getIdVisitor()},
				new EventMapper());
		
		
				
		return events;
		
	}
	
	/**
	 * 
	 * update seatsavaliable with -1
	 * return true if it is done correctly and false if not
	 * 
	 * @param e
	 * @return
	 * @throws SQLException
	 */

public boolean updateMinusSeatsAvailableEventDAO(EventSignUp e){
		
		int affectedRows = 0;
				
		affectedRows=jdbcTemplateObject.update(queryProperties.getProperty("update_seats_event_dec"), e.getIdEvent());
		
		if(affectedRows==1){
			return true;
			}
		else
		{
			return false;
		}
		
		
	}
/**
 * update seatsavaliable with +1
 * return true if it is done correctly and false if not
 * 
 * @param e
 * @return
 * @throws SQLException
 */

public boolean updateSumSeatsAvailableEventDAO(EventSignUp e){
	
	int affectedRows = 0;
		
	affectedRows=jdbcTemplateObject.update(queryProperties.getProperty("update_seats_event_sum"), e.getIdEvent());
	
	
	if(affectedRows==1){
		return true;
		}
	else
	{
		return false;
	}
	
		
}
/**
 * 
 * return true if SeatsAvailable>0 and false if not
 * 
 * @param e
 * @return
 * @throws SQLException
 */

public boolean checkSeatsAvailableDAO(EventSignUp e) throws SQLException{
	
	List<Event> events = new ArrayList<Event>();
	
	events=jdbcTemplateObject.query(queryProperties.getProperty("checkSeatsAvailable"),
			new Object[]{e.getIdEvent()},
			new EventMapper());
	
	
		if(events.get(0).getSeatsAvailable()>0)
	{
		return true;
	}
	else{
		return false;
	}

	
}

/**
 * 
 * Unregister all the visitors for a particular event
 * return true if it is done correctly and false if not
 * 
 * @param idEvent
 * @return
 * @throws SQLException
 */

public boolean unregisterAllVisitorforEvent(final int idEvent) throws ClassNotFoundException, SQLException {
	boolean bool=false;
		
	int affectedRows = jdbcTemplateObject.update(queryProperties.getProperty("unRegisterAllVisitorToEvent"), 
			new PreparedStatementSetter(){
		@Override
		public void setValues(PreparedStatement statement)
				throws SQLException {
			statement.setInt(1, idEvent);
			
		}
	});
	
	if(affectedRows>=0){
		bool=true;
	}
	return bool;
	
	
}
	
	
}
